// file Term.java
// 발생 텀별로 확율 조사
// compiler configuration : -classpath ".." -d ".."
//=====================================

package z_app_lotto2;

import java.awt.BorderLayout;
import java.awt.Rectangle;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.InternalFrameAdapter;
import javax.swing.event.InternalFrameEvent;

import java.util.*;

public class Term extends JInternalFrame implements ActionListener {
	String fileName;
    Data d;
    Vector data;
    JButton button;
    JTextArea doc;
    JLabel jLabel1;
    JScrollPane jScrollPane2;
    JTextField hitField;
    JPanel panel;
    int val[][];
    int termNo[][];

    public Term() {
		super("발생빈도 List",true, true, true, false);
		fileName = this.getClass().getSimpleName() + ".cfg";
	
		addInternalFrameListener(new InternalFrameAdapter() {
			@Override
			public void internalFrameOpened(InternalFrameEvent e) {
				setBounds(new InterWindowAdapter(fileName).read());
			}
			@Override
			public void internalFrameClosing(InternalFrameEvent e) {
				InterWindowAdapter adapter = new InterWindowAdapter(fileName);
				int x = getX();
				int y = getY();
				int w = getWidth();
				int h = getHeight();
				Rectangle rect = new Rectangle(x,y, w, h);
				adapter.save(rect);
			}
		});
		getContentPane().setLayout(new BorderLayout());
		
		this.setVisible(true);
		
        initComponents();
        init();
    }

    public void init() {
        boolean find = false;
        String colName[] = {"번호", "발생횟수", "확율"};
        String dd = new String();

        int no = 0, base = 0, first = 0, term = 0, totCnt = 0;
        double rate = 0.0;
        int termCnt = 0;
        int outputCnt = 0;
        int hit=Integer.parseInt(hitField.getText());
        doc.setText("");


        button.addActionListener(this);

        d = new Data("src/z_app_lotto2/lotto.dat");
        data = d.getData();

        val = new int[data.size()][8];
        termNo = new int[46][data.size()];

        // data compute
        for (int i = 0; i < data.size(); i++) {
            Vector tmp = (Vector) data.get(i);
            for (int j = 0; j < tmp.size(); j++) {
                val[i][j] = Integer.parseInt((String) tmp.get(j));
            }
        }

        // term
        for (no = 1; no < 46; no++) {
            find = false;
            first = 0;
            termCnt = 1;
            termNo[no][0] = no;
            outputCnt = 0;
            for (int i = 0; i < data.size(); i++) {
                for (int j = 1; j < 8; j++) {
                    if (val[i][j] == no) {
                        if (first == 0) {
                            first = val[i][0];
                        } else {
                            termNo[no][termCnt] = first - val[i][0];
                            first = val[i][0];
                            termCnt++;
                        }
                        break;
                    }
                }
            }
        }

        base = val[0][0] + 1;
        doc.append("base=" + base + "\n");

        // 최종 출현 횟차 검색
        for (no = 1; no < 46; no++) {
            find = false;
            for (int i = 0; i < data.size() && find == false; i++) {
                for (int j = 1; j < 8; j++) {
                    if (val[i][j] == no) {
                        find = true;
                        first = val[i][0];
                        break;
                    }
                }
            }

            term = base - first;
            totCnt = 0;
            for (int i = 1; i < data.size(); i++) {
                if (termNo[no][i] == term) {
                    totCnt++;
                }
            }

            rate = (totCnt * 1.0) / data.size() * 100;
            if (totCnt > hit) {
                dd = "" + no + " : " + first + ", " + term + ", [" + totCnt +
                     "] [" + rate + "]\n";
                doc.append(dd);
                outputCnt++;
            }

        }
        doc.append("\n" + "count=" + outputCnt);
    }

    public void actionPerformed(ActionEvent e) {
        init();
    }

    private void initComponents() {
        jLabel1 = new javax.swing.JLabel();
        hitField = new javax.swing.JTextField("3");
        button = new javax.swing.JButton();
        panel = new javax.swing.JPanel();
        jScrollPane2 = new javax.swing.JScrollPane();
        doc = new javax.swing.JTextArea();

        setLayout(null);

        jLabel1.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        jLabel1.setText("발생빈도");
        add(jLabel1);
        jLabel1.setBounds(20, 10, 70, 16);

        add(hitField);
        hitField.setBounds(100, 10, 100, 21);

        button.setText("\ud655\uc778");
        add(button);
        button.setBounds(200, 10, 70, 25);

        panel.setLayout(null);

        doc.setColumns(25);
        doc.setRows(20);
        jScrollPane2.setViewportView(doc);

        add(jScrollPane2);
        jScrollPane2.setBounds(10, 40, 550, 500);

    }

    public static void main(String args[]) {
        JFrame frame = new JFrame();
        Term t = new Term();

        frame.setSize(600, 600);
        frame.getContentPane().add(t);

        frame.setVisible(true);

    }


}
